
import pygtk
pygtk.require('2.0')
import gtk

import _text
import _emulator
import thread

EScreen = 0#Screen.
EApplicationWindow = 0#Window that fills the entire screen.
EStatusPane = 0#Indicates common components for most of the applications.
EMainPane = 0#The application main pane is used in all the applications.
EControlPane = 0#Control pane.
ESignalPane = 0#The signal pane is used to indicate signal strength.
EContextPane = 0#The context pane is used to indicate an active application.
ETitlePane = 0#Used to indicate the subject or the name of the main pane content.
EBatteryPane = 0#The battery pane is used to indicate battery strength.
EUniversalIndicatorPane = 0#The universal indicator pane is used to indicate items that require the s attention while browsing applications.
ENaviPane = 0#The navi pane is used to indicate navigation within an application, to provide context sensitive information to the user while entering or editing data, or to show additional information.
EFindPane = 0#A fixed find pane is used with lists instead of the find pop-up window.
EWallpaperPane = 0#Wallpaper pane.
EIndicatorPane = 0#The universal indicator pane is used to indicate items that require the s attention while browsing applications.
EAColumn = 0#Used generally to display small sized graphics or heading texts.
EBColumn = 0#Used generally to display large sized icons or heading texts.
ECColumn = 0#Used generally to display data entered by the user. Overlaps with the D column.
EDColumn = 0#Used generally to display additional icons. Overlaps with the C column.
EStaconTop = 0#Top part of status and control panes in landscape layout.
EStaconBottom = 0#Bottom part of status and control panes in landscape layout.
EStatusPaneBottom = 0#Bottom part of status pane in landscape layout.
EControlPaneBottom = 0#Bottom part of control pane in landscape layout.
EControlPaneTop = 0#Top part of control pane in landscape layout.
EStatusPaneTop = 0#Top part of status pane in landscape layout.useruser


def _init_gui():
     _emulator._uiThreadID = thread.get_ident()
     _emulator._w = gtk.Window(gtk.WINDOW_TOPLEVEL)
     

class Application(object):
    """A single implicit instance of this type always exists when appuifw module is present and can be
    referred to with the name app. New instances cannot be created by a Python program.
    """

    def _get_body(self):
         return self._body
    def _set_body(self, value):
         if value is None:
              value = _text.Text()

         if self._ctrl is not None:
              self._ctrl.hide()
              self._ctrl_box.remove(self._ctrl)
                   
         self._body = value
         self._ctrl = value._ctrl
         self._ctrl_box.pack_start(self._ctrl, True, True, 0)
         self._ctrl.show()
                   
    body = property(_get_body, _set_body, doc="body attribute")

    def _get_exit_key_handler(self):
         return self._exit_key_handler
    def _set_exit_key_handler(self, value):
         self._exit_key_handler = value
    exit_key_handler = property(_get_exit_key_handler, _set_exit_key_handler, doc="exit_key_handler attribute")

    def _get_menu(self):
         return self._menu
    def _set_menu(self, value):
         self._menu = value
         self._SetMenu(value)
    menu = property(_get_menu, _set_menu, doc="menu attribute")

    def _get_screen(self):
         return self._screen
    def _set_screen(self, value):
        self._screen = value
	if value == "normal":
	    self._upper_box.show()
	    self._button_box.show()
	if value == "large":
	    self._upper_box.hide()
	    self._button_box.show()
	if value == "full":
	    self._upper_box.hide()
	    self._button_box.hide()
    screen = property(_get_screen, _set_screen, doc="screen attribute")

    def _get_focus(self):
         return self._focus
    def _set_focus(self, value):
         self._focus = value
    focus = property(_get_focus, _set_focus, doc="focus attribute")

    def _get_orientation(self):
         return self._orientation
    def _set_orientation(self, value):
         self._orientation = value
    orientation = property(_get_orientation, _set_orientation, doc="orientation attribute")

    def _get_title(self):
         return self._title
    def _set_title(self, value):
         self._title = value
	 self._title_label.set_text(value)
    title = property(_get_title, _set_title, doc="title attribute")

    def __init__(self):

        _emulator._uiThreadID = thread.get_ident()

        self._w = _emulator._w
        self._w.connect('delete_event', self._OnDelete)
        self._w.connect('focus_in_event', lambda w, e, d = None : self._OnFocus(1))
        self._w.connect('focus_out_event', lambda w, e, d = None : self._OnFocus(0))
        self._w.set_resizable(False)
        self._w.set_size_request(_emulator._screenSizeEmul[0], _emulator._screenSizeEmul[1])


#	self._frame.Bind(wx.EVT_KEY_DOWN, self._OnKeyDown)

	#build the UI
	self._buildUI()

        self._tabSelectCb = None

	#Public fields
        self._body = None
        self._exit_key_handler = None
        self._menu = []
        self._screen = u"normal"
        self._title = u"Python Application"
        self._focus = None
        self._orientation = 'automatic'

        self._w.show()

    def _buildUI(self):
        self._main_box = gtk.VBox(False, 0)
	
	
	self._title_label = gtk.Label(u"Python");
	self._title_label.show()
	
	uw = _emulator._screenSizeEmul[0]

        self._tabs = gtk.Notebook()
        self._tabs.set_size_request(int(uw/10), 25) #!!!!
        self._tabs.connect('switch-page', self._OnTabChange, None)
        self._tabs.show()
        self._tabs_box = gtk.HBox(False, 0)
        self._tabs_box.pack_start(self._tabs, True, True, 0)
        self._tabs_box.hide()
	
	self._title_box = gtk.VBox(False, 0)
	self._title_box.pack_start(self._title_label, True, True, 0)
	self._title_box.pack_start(self._tabs_box, True, True, 0)
	self._title_box.show()
		
	self._signal_ind = gtk.Frame()
	self._signal_ind.set_size_request(int(uw/15), 25) #!!!!
	self._signal_ind.show()
	
	self._app_icon = gtk.Frame()
	self._app_icon.set_size_request(int(uw/7), 25) #!!!!
	self._app_icon.show()
	
	self._batt_ind = gtk.Frame()
	self._batt_ind.set_size_request(int(uw/15), 25) #!!!!
	self._batt_ind.show()
	
	self._upper_box = gtk.HBox(False, 0)
	self._upper_box.pack_start(self._signal_ind, False, False, 0)
	self._upper_box.pack_start(self._app_icon, False, False, 0)
	self._upper_box.pack_start(self._title_box, True, True, 0)
	self._upper_box.pack_start(self._batt_ind, False, False, 0)
	self._upper_box.show()
	

        self._ctrl = None
        self._ctrl_box = gtk.HBox(False, 0)
        self._ctrl_box.show()

        self._exit_bn = gtk.Button(" Exit ")
        self._exit_bn.connect('clicked', self._OnExit, None)
        self._exit_bn.show()

        self._menu = None
	self._options_bn = gtk.Button("Options")
        self._options_bn.connect('clicked', self._OnOptions, None)
        self._options_bn.show()

	self._button_box = gtk.HBox(False, 0)
	self._button_box.pack_start(self._options_bn, True, False, 0)
	self._button_box.pack_start(self._exit_bn, True, False, 0)
        self._button_box.show()

        self._main_box = gtk.VBox(False, 0)
	self._main_box.pack_start(self._upper_box, False, False, 10)
	self._main_box.pack_start(self._ctrl_box, True, True, 10)
	self._main_box.pack_start(self._button_box, False, False, 10)
        self._main_box.show()

	self._w.add(self._main_box)

    def activate_tab(self, index):
        """Activates the tab index counting from zero."""
        self._tabs.set_current_page(index)
        
    def full_name(self):
        """Retrieves the full name, in Unicode, of the native application in whose context the current
        Python interpreter session runs.
        """
        return u"Python S60 emulator"

    def uid(self):
        """Returns the UID, in Unicode, of the native application in whose context the current Python
        interpreter session runs.
        """
        return u""
        
    def set_exit(self):
        """Requests a graceful exit from the application as soon as the current script execution returns."""
        pass
        
    def set_tabs(self, tab_texts, callback = None):
        """Sets tabs with given names on them in the navigation bar; 
      
        tab_texts is a list of Unicode
        strings. When the users navigate between tabs, callback gets called with the index of the
        active tab as an argument. Tabs can be disabled by giving an empty or one-item tab_texts
        list.
        """
        self._tabs_box.hide()
        self._tabs_box.remove(self._tabs)

        self._tabs = gtk.Notebook()
        self._tabs.set_scrollable(True)
        self._tabs.set_show_border(False)
        self._tabs.can_focus = False
        self._tabs.set_size_request(int(_emulator._screenSizeEmul[0]/10), 25) 
        self._tabs.connect('switch-page', self._OnTabChange, None)
        self._tabs.show()
        self._tabs_box.pack_start(self._tabs, True, True, 0)
	
	for tab in tab_texts:
            frame = gtk.Frame()
            frame.show()
	    self._tabs.append_page(frame, gtk.Label(tab));
	
        if len(tab_texts) > 0 :
            self._tabs_box.show()

        self._tabSelectCb = callback

    def layout(self, layout_id):
        """Note: Available from S60 2ndEd FP3 onwards (inclusive).
        Returns as a tuple the size and the position of the requested layout id.
        The position is given from the top left corner.
        """
        return ((100,100), (0,20))
        
    def _OnTabChange(self, tabs, page, pageNum, data=None):
        if self._tabSelectCb:
            self._tabSelectCb(pageNum)
	    
    def _OnExit(self, widget, data=None):
        """Called upon wx.Frame close"""
        if self._exit_key_handler:
	    self._exit_key_handler()
	else:
            gtk.main_quit()

    def _menuPosFunc(self, menu, data=None):
         rect = self._options_bn.get_allocation()
         origin = self._options_bn.window.get_origin()
         return (origin[0]+rect.x, origin[1]+rect.y, False)
	    
    def _OnOptions(self, widget, data=None):
	if self._menu_ctrl:
	    self._menu_ctrl.popup(None, None, self._menuPosFunc, 0, gtk.get_current_event_time())
	
    def _SetMenu(self, items):
	self._menu_ctrl = gtk.Menu()
	self._AddSubMenu(self._menu_ctrl, items)
        self._menu_ctrl.show()
	
    def _AddSubMenu(self, menu, items):
	for name, content in items:
            if isinstance(content, tuple):
                submenu = gtk.Menu()
                self._AddSubMenu(submenu, content)
                item = gtk.MenuItem(name);
                item.set_submenu(submenu)
                menu.append(item)
                item.show()
                submenu.show()
            else:
                item = gtk.MenuItem(name);

                def cb(x):
                    return lambda event:x()
                
                item.connect_object("activate", cb(content), None)
                menu.append(item)
                item.show()

    def _OnFocus(self, received):
         if self._focus:
              self._focus(received)
				
    def _OnKeyDown(self, event):
#	self.body.OnKeyDown(event)
        pass

    def _OnDelete(self, widget, event, data=None):
        gtk.main_quit()
        return False
	
    def _getWindow(self):
        return self._w
	
#initialize GUI
_init_gui()
